home *** CD-ROM | disk | FTP | other *** search
- // Update Notifier
- // By Todd Long <longfocus@gmail.com>
- // http://www.longfocus.com/firefox/updatenotifier/
-
- const CC = Components.classes;
- const CI = Components.interfaces;
-
- const ITEM = "urn:mozilla:item:";
-
- const NOTIFY_UPDATE_CHANGE = "UN:update-change";
-
- function UNManager() {}
- UNManager.prototype = {
- _items: new Array(),
- _firstTime: true,
- _check: false,
- _install: false,
-
- set check(aCheck) { this._check = aCheck; },
- set install(aInstall) { this._install = aInstall; },
-
- getNumItems: function()
- {
- return this._items ? this._items.length : 0;
- },
-
- getNumItemsByType: function(aType)
- {
- var num = 0;
-
- for (var i = 0; i < this._items.length; i++)
- if (this._items[i].type == aType)
- num++;
-
- return num;
- },
-
- getItemByIndex: function(aNum)
- {
- return this._items ? this._items[aNum] : null;
- },
-
- checkUpdates: function()
- {
- var items = this._em.getItemList(CI.nsIUpdateItem.TYPE_ANY, {});
- this._em.update(items, items.length, false, null);
- },
-
- installUpdates: function()
- {
- var itemList = new Array();
-
- for (var i = 0; i < this._items.length; i++)
- itemList.push(this._em.getItemForID(this._items[i].id));
-
- if (itemList.length > 0)
- this._em.addDownloads(itemList, itemList.length, true);
- },
-
- load: function()
- {
- if (this._firstTime)
- {
- this._firstTime = false;
-
- this._em = CC["@mozilla.org/extensions/manager;1"].getService(CI.nsIExtensionManager);
- this._cs = CC['@mozilla.org/consoleservice;1'].getService(CI.nsIConsoleService);
- this._observer = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
-
- // Adds observer for item updates
- this._em.datasource.AddObserver(this);
-
- if (this._check)
- this.checkUpdates();
- }
-
- // Checks the RDF for updates
- this._loadItemUpdates();
- },
-
- _loadItemUpdates: function()
- {
- var rs = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
- var cu = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils);
- var ds = this._em.datasource;
-
- var res = rs.GetResource(ITEM+"root");
- var container = null;
-
- if(cu.IsSeq(ds, res)) {
- container = CC["@mozilla.org/rdf/container;1"].getService(CI.nsIRDFContainer);
- container.Init(ds, res);
- }
- else
- container = cu.MakeSeq(ds, res);
-
- // Gets the datasource resources
- var elements = container.GetElements();
-
- while (elements.hasMoreElements()) {
- var element = elements.getNext().QueryInterface(CI.nsIRDFResource);
- var item = this._getItemUpdateInfo(this._em.datasource, element);
-
- if (item.url != null && item.version != null)
- this._addItemUpdate(item);
- }
- },
-
- _getItemUpdateInfo: function(aDataSource, aSource)
- {
- var arcCursor = aDataSource.ArcLabelsOut(aSource);
- var item = new Object();
-
- item.id = aSource.Value.replace(ITEM, "");
- item.url = null;
- item.version = null;
-
- while(arcCursor.hasMoreElements())
- {
- thisArc = arcCursor.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
-
- var target = aDataSource.GetTarget(aSource, thisArc, true);
- var rdfLiteral = null;
-
- if (target instanceof CI.nsIRDFLiteral)
- rdfLiteral = target.Value;
-
- if (thisArc.Value.indexOf("availableUpdateURL") > -1)
- item.url = rdfLiteral;
- else if (thisArc.Value.indexOf("availableUpdateVersion") > -1)
- item.version = rdfLiteral;
- }
-
- return item;
- },
-
- _addItemUpdate: function(aItem)
- {
- // Double check for valid item
- if (aItem && aItem.url != null && aItem.version != null)
- {
- var item = this._em.getItemForID(aItem.id);
- var uItem = CC["@longfocus.com/updatenotifier/item;1"].createInstance(CI.nsIUNItem);
-
- // Update item
- uItem.id = aItem.id;
- uItem.type = (item.type == 2 ? "extension" : "theme");
- uItem.url = aItem.url;
- uItem.version = aItem.version;
-
- /*
- this._cs.logStringMessage("--- New Update ---" + "\n" +
- uItem.id + "\n" +
- uItem.type + "\n" +
- uItem.url + "\n" +
- uItem.version + "\n" +
- "---");
- */
-
- var index = this._getItemIndex(uItem);
- var updated = false;
-
- if (index > -1) {
- var item = this._items[index];
-
- if (item.url != uItem.url || item.version != uItem.version) {
- this._items[index] = uItem;
- updated = true;
- }
- }
- else {
- this._items[this._items.length] = uItem;
- updated = true;
- }
-
- // Check for updated item
- if (updated)
- {
- // Check for install
- if (this._install)
- // Installs updates
- this.installUpdates();
- else
- // Notify of update change
- this._observer.notifyObservers(
- uItem, NOTIFY_UPDATE_CHANGE,
- this.getNumItemsByType(uItem.type));
- }
- }
- },
-
- _removeItemUpdate: function(aItem)
- {
- var index = this._getItemIndex(aItem);
-
- if (index > -1)
- {
- var item = this._items[index];
- this._items.splice(index, 1);
-
- // Notify for removed update
- this._observer.notifyObservers(
- item, NOTIFY_UPDATE_CHANGE,
- this.getNumItemsByType(item.type));
- }
- },
-
- _getItemIndex: function(aItem)
- {
- var index = -1;
-
- for (var i = 0; i < this._items.length && index == -1; i++)
- if (this._items[i].id == aItem.id)
- index = i;
-
- return index;
- },
-
- onChange: function(aDataSource, aSource, aProperty, aOldTarget, aNewTarget)
- {
- var pv = aProperty.QueryInterface(CI.nsIRDFResource).Value;
-
- if (pv.indexOf("availableUpdateURL") > -1 || pv.indexOf("availableUpdateVersion") > -1) {
- var item = this._getItemUpdateInfo(aDataSource, aSource);
- this._addItemUpdate(item);
- }
- },
-
- onUnassert: function(aDataSource, aSource, aProperty, aTarget)
- {
- var pv = aProperty.QueryInterface(CI.nsIRDFResource).Value;
-
- if (pv.indexOf("availableUpdateURL") > -1 || pv.indexOf("availableUpdateVersion") > -1) {
- var item = this._getItemUpdateInfo(aDataSource, aSource);
- this._removeItemUpdate(item);
- }
- },
-
- onAssert: function(aDataSource, aSource, aProperty, aTarget) {},
- onBeginUpdateBatch: function(aDataSource) {},
- onEndUpdateBatch: function(aDataSource) {},
- onMove: function(aDataSource, aOldSource, aNewSource, aProperty, aTarget) {},
-
- QueryInterface: function(iid)
- {
- if (!iid.equals(Components.interfaces.nsIUNManager) &&
- !iid.equals(Components.interfaces.nsISupports))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
- }
-
- var myModule = {
- firstTime: true,
-
- myCID: Components.ID("{0090c2b0-9e45-11da-a746-0800200c9a66}"),
- myDesc: "Manager for new available updates",
- myProgID: "@longfocus.com/updatenotifier/manager;1",
- myFactory: {
- createInstance: function (outer, iid) {
- if (outer != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
-
- return (new UNManager()).QueryInterface(iid);
- }
- },
-
- registerSelf: function (compMgr, fileSpec, location, type)
- {
- if (this.firstTime) {
- this.firstTime = false;
- throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
- }
-
- compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
- },
-
- getClassObject: function (compMgr, cid, iid)
- {
- if (!cid.equals(this.myCID))
- throw Components.results.NS_ERROR_NO_INTERFACE;
-
- if (!iid.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- return this.myFactory;
- },
-
- canUnload: function(compMgr) { return true; }
- };
-
- function NSGetModule(compMgr, fileSpec) { return myModule; }
-